package com.jy.appmarket.notification;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import com.jy.app.market.idata.data.Upgrade;
import com.jy.appmarket.R;
import com.jy.appmarket.download.DownloadItem;
import com.jy.appmarket.download.DownloadService;
import com.jy.appmarket.statistics.ApkSource;
import com.jy.appmarket.utils.Prefs;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;

/*
 * 应用市场自己升级通知
 */
public class UpdateNotification {

	public static final int	UPDATE_NOTIFY_ID = 123;

	private static boolean isShowTime(Context context) {

		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
		String date = dateFormat.format(calendar.getTime());
		
		if(hour >= 19 && hour < 22){
			if(date.equals(Prefs.getUpgradeNotifyNight(context))){
				return false;
			}else{
				Prefs.saveUpgradeNotifyNight(context, date);
				return true;
			}
		}
		if(hour >= 12 && hour < 14){
			if(date.equals(Prefs.getUpgradeNotifyNoon(context))){
				return false;
			}else{
				Prefs.saveUpgradeNotifyNoon(context, date);
				return true;
			}
		}
		return false;
	}

	/**
	 * 显示通知
	 * @param context
	 * @param force 是否考虑显示通知的时间段，，true为不考虑，false为考虑
	 * @param UpdateInfo 软件更新信息
	 */
	public static void showUpdateNotification(Context context, boolean force, Upgrade upgrade) {
		
		if(!force) {
			if (!isShowTime(context)) {
				return;
			}
		}

		NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
		builder.setSmallIcon(R.drawable.notify_icon)
		.setAutoCancel(true)
		.setStyle(new NotificationCompat.BigTextStyle().bigText(upgrade.getTitle()))
		.setContentTitle(getSpecialString(context.getString(R.string.notification_title), 4, 6, 0XFFFF9500))
		.setContentText(upgrade.getDetail())
		.setTicker(upgrade.getTitle())
		.setDefaults(Notification.DEFAULT_SOUND)
		.setPriority(Notification.PRIORITY_MAX)
		.setOnlyAlertOnce(true);
		
		Intent intent = new Intent(context, DownloadService.class);
		intent.setAction(DownloadService.ACTION_START_DOWNLOAD);
		intent.putExtra(DownloadService.EXTRA_DOWNLOADMODEL, getDownloadItem(upgrade, context));
		intent.putExtra(DownloadService.EXTRA_OPEN_DOWNLOADMGR, true);
		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		
		PendingIntent pendingInstall = PendingIntent.getService(context, tag,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(pendingInstall);
		NotificationManager nm = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		nm.notify(UPDATE_NOTIFY_ID, builder.build());
	}

	public static SpannableStringBuilder getSpecialString(String str, int start, int end, int color) {
		SpannableStringBuilder ssb = new SpannableStringBuilder(str);
		ssb.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		return ssb;
	}

	public static void clearUpdateNotify(Context context) {
		NotificationManager nm = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		nm.cancel(UPDATE_NOTIFY_ID);
	}
	
	private static DownloadItem getDownloadItem(Upgrade upgrade, Context context) {
		DownloadItem dlItem = new DownloadItem();
		dlItem.setName(context.getString(R.string.app_name));
		dlItem.setIconUrl(upgrade.getIcon());
		dlItem.setPkgName(context.getPackageName());
		dlItem.setAutoInstall(true);
		dlItem.setHidden(false);
		dlItem.setDlUrl(upgrade.getUrl());
		dlItem.setVersionCode(upgrade.getVersionCode());
		dlItem.setSource(new ApkSource("upgrade").toSourceString());
		return dlItem;
	}
}
